export default class orc_utils {
     /**
     * 随机一个整数数字
     * @param min 从min开始
     * @param max 到max结束
     * @returns {*}
     */
    public static get_random(min:number, max:number):number 
    {
        if (isNaN(min) || isNaN(max))return 0;
        if (min >= max) return min;
        var temp = (Math.random() * (max - min + 1) ) + min ;
        temp = Math.floor(temp);
        if(temp == max +1)
        {
            return max;
        }
        else
        {
            return temp;
        }
    }

    /**
     * 讲数字number转化成string,20300 = 2.03万
     * @param number 数字
    */
   public static string_number(number:number):string
   {
        var number_string = '';
        if(number >= 10000)
        {
            number_string = String((number/10000).toFixed(2))+'万';
        }
        else
        {
            number_string = String(number);
        }
        return number_string;
    }

    /**
     * 将秒时间转换为 03:15这种格式
     * @param time 
     */
    public static clock_convert(time:number):Array<string>
    {        
        let minus:String = ('00' + String(Math.floor(Number(time) / 60))).slice(-2);
        let second:String = ('00' + String(Number(time) % 60)).slice(-2);
        let clock_string = [];
        clock_string[0] = minus;
        clock_string[1] = second;
        return clock_string;        
    }

    /**
     * 将json数据转化成formdata
     * @param json 
    */
    public static json_to_formdata(json:any)
    {
        let form = new FormData();   
        for(let key in json)
        {
            form.append(key,json[key])
        }
        return form;
    }

    /**
     * 传入时间戳,返回日期。如果是当天，则显示几小时前 
    */
    public static time_to_data(time:any)
    {
        let time_param = Number(time);
        var timestamp = (new Date()).getTime()/1000;    //获取当前时间戳 单位为S;
        let return_date:string;
        console.log('timestamp:',timestamp,time);
        
        if(timestamp - time >= (24 * 3600))
        {
            //隔天了
            let m =  (new Date(time_param*1000)).getMonth();//time_param.getMonth();
            let d =  (new Date(time_param*1000)).getDate();//time_param.getMonth();//time_param.getDate();
            
            return_date = String(m+1)+'月'+String(d)+'日';            
        }
        else
        {
            //没有超过24小时
            let time_cal = Math.floor((timestamp - time_param)/3600);
            if(time_cal <= 0)
            {
                return_date = '1小时前';
            }
            else if(time_cal >= 24)
            {
                return_date = '23小时前';
            }
            else
            {
                return_date =   String(time_cal) + '小时前';
            }
        }
        return return_date;
    }

    /**
     * csv数据转变为json
     * @param strData     
     */
    public static CSVToArray(strData) 
    {
        var strDelimiter = ",";
        var objPattern = new RegExp(
        (
                "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
                "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
                "([^\"\\" + strDelimiter + "\\r\\n]*))"
                ),
            "gi"
        );       
        var arrData = [[]];
        var arrMatches = null;
        while (arrMatches = objPattern.exec( strData ))
        {
            var strMatchedDelimiter = arrMatches[ 1 ];
            if( strMatchedDelimiter.length &&(strMatchedDelimiter != strDelimiter))
            {
                arrData.push( [] );
            }
            if (arrMatches[ 2 ]){
                var strMatchedValue = arrMatches[ 2 ].replace(
                    new RegExp( "\"\"", "g" ),
                    "\""
                );
            } else {
                var strMatchedValue = arrMatches[ 3 ];
            }
            console.log('strMatchedValue:',arrData.length,strMatchedValue);
            
            arrData[ arrData.length - 1 ].push( strMatchedValue );
        }
        if(arrData.length>0){
            arrData.pop();
        }
        return arrData;
    }
}
